"use strict";
let _y = 0; //初始位置
let _x = 0; //初始位置
let _ph = 0; //初始宽度
let _pw = 0; //初始宽度
let _obj = ``; //狼对象
let _t; //时间戳
let _wolfL = 1; //时间戳
let _moveX = ""; //X轴
let _moveY = ""; //Y轴
let _moveT = false; //摩托车移动时间戳
$(function() {
    _ph = document.body.clientHeight - 60; //初始宽度
    _pw = document.body.clientWidth; //初始宽度

    console.log("%c%s",
        "color: #fff; background: chocolate; font-size: 12px;padding:2px 10px;border-radius:2px;",
        "赛车");


    //键盘事件ArrowUp ArrowLeft ArrowRight ArrowDown
    $(document).on({
        keydown: function(e) { //按下
            //console.log(e.key);
            e = e || Event;
            switch (e.key) {
                case "w": //上
                case "ArrowUp": //上
                case "s": //下
                case "ArrowDown": //下
                    _moveY = e.key;
                    //console.log(_moveT);
                    if (!_moveT) {
                        //console.log(_moveT);
                        moveSaiche();
                    }
                    break;

                case "a": //左
                case "ArrowLeft": //左
                case "d": //右
                case "ArrowRight": //右
                    _moveX = e.key;
                    //console.log(_moveT);
                    if (!_moveT) {
                        moveSaiche();
                    }
                    break;
            };

        },
        keyup: function(e) { //按下
            //console.log(e.key);
            e = e || Event;
            switch (e.key) {
                case "w": //上
                case "ArrowUp": //上
                case "s": //下
                case "ArrowDown": //下
                    _moveY = "";
                    break;

                case "a": //左
                case "ArrowLeft": //左
                case "d": //右
                case "ArrowRight": //右
                    _moveX = "";
                    break;
            };
        }
    });

    //初始化舞台
    initAll();

    //射杀狼
    $(document).on("click", ".wolf", function(e) {
        _obj = $(this);
        ////console.log(e.pageX);
        ////console.log(e.pageY);
        //$(".wolf").remove(); //删除狼
        $("body").append(`<div class="bullet fixed" style="left:${$("#saiche").offset().left+15}px;top:${$("#saiche").offset().top}px"></div>`);
        $(".bullet").animate({ top: e.pageY + "px", left: e.pageX + "px" }, 100, function() {
            _obj.remove(); //删除狼
            $(".bullet").remove(); //删除狼
            $("#num").text(parseInt($("#num").text()) + 1);
        })
    });


    //下一关
    $("#next").click(function() {

        $(".wolf").remove(); //删除狼
        $(".saiche").remove(); //删除赛车
        //初始化背景
        background();
        clearInterval(_t); //清除时间戳
        _wolfL++; //添加一条狼
    });


    //开始
    $("#begin").click(function() {
        //初始化摩托车
        initSaiche();

        //初始化狼
        wolf(_wolfL);
    });

    //重开
    $("#beginA").click(function() {
        $(".wolf").remove(); //删除狼
        $(".saiche").remove(); //删除赛车
        //初始化背景
        background();
        clearInterval(_t); //清除时间戳
        _wolfL = 1; //添加一条狼
    });
});

//初始化所有元素
function initAll() {
    //初始化背景
    background();
};

//初始化背景
function background() {
    let _b = ((Math.random() * 10).toString()).slice(0, 1);
    do {
        _b = ((Math.random() * 10).toString()).slice(0, 1);
    }
    while ($("body").css("background").indexOf(`${_b==0?1:_b}`) > -1)
    $("body").css("background", `url(./img/backImg/${_b==0?1:_b}.jpg)`)
};


//初始化摩托车的位置
function initSaiche() {
    _x = parseFloat(_pw / 2 - 15);
    _y = parseFloat(_ph - 30);
    if ($("#saiche").length == 0)
        $("body").append(`<div id="saiche" class="fixed saiche" style="left:${_pw/2-15}px;top:${_ph-30}px"></div>`);
};


//移动车子
function moveSaiche() {
    //console.log(_moveT)
    _moveT = setInterval(function() {
        if (_moveY || _moveX) {
            //console.log(_moveY);
            //console.log(_moveX);
            switch (_moveY) {
                case "w": //上
                case "ArrowUp": //上
                    if (_y < 1) //最小高度
                        return
                    _y--;
                    moveSaicheY(_y); //移动赛车
                    break;
                case "s": //下
                case "ArrowDown": //下
                    if (_y == _ph) //最大高度
                        return
                    _y++;
                    moveSaicheY(_y); //移动赛车
                    break;
            };
            switch (_moveX) {
                case "a": //左
                case "ArrowLeft": //左
                    if (_x < 1) //最小宽度
                        return
                    _x--;
                    moveSaicheX(_x); //移动赛车
                    break;
                case "d": //右
                case "ArrowRight": //右
                    if (_x == _pw) //最大宽度
                        return
                    _x++;
                    moveSaicheX(_x); //移动赛车
                    break;
            };
        } else {
            //console.log(_moveX)
            clearInterval(_moveT);
            _moveT = false;
        }
    }, 100);
};


//移动车子Y轴
function moveSaicheY(y) {
    //console.log(y);
    $("#saiche").css({ top: y });
};

//移动车子x轴
function moveSaicheX(x) {
    //console.log(x);
    $("#saiche").css({ left: x });
};

//随机生成狼
function wolf(wolfL) {
    let _p = 0;
    let _l = 0; //狼的序列
    _t = setInterval(function() {
        if ($(".wolf").length < wolfL) {
            _l++;
            _p = position();
            // //console.log(_p);
            $("body").append(`<div class="fixed 12 wolf wolf${_l}"  id="123" style="left:${_p.x}px;top:${_p.y}px"></div>`);
        }
    }, 200);
};

//生成狼的坐标
function position() {
    let json = {
        x: 0,
        y: 0
    };
    let _x = 1; //几位数的宽
    let _y = 1; //几位数的高
    let _px = 0; //x坐标
    let _py = 0; //y坐标
    let _l = 0; //长度
    do {
        _px = Math.random(); //x坐标
        ////console.log(_px);
        _x = 1;
        _l = _pw.toString().length;
        //console.log(_l);
        for (let i = 0; i < _l; i++) {
            _x += "0";
        };
        _px = parseInt(_px * parseInt(_x));
    }
    while (_px >= _pw - 60)
    //console.log("-------------------");
    //console.log(_pw + "----" + _ph);
    //console.log("-------------------");
    do {
        _py = Math.random(); //y坐标
        // console.log(_py);
        _y = 1;
        _l = _ph.toString().length;
        //console.log(_l);
        for (let i = 0; i < _l; i++) {
            _y += "0";
        };
        _py = parseInt(_py * parseInt(_y));
    }
    while (_py >= _ph - 80)

    json.x = _px;
    json.y = _py;
    //console.log(json);
    return json;
};